package com.xbai.intervention.client;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * @author jxbai
 * @desc
 * @date 2021/1/14 0014
 */
@Component
public class UserServiceClient {

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(threadPoolKey = "springHealthGroup",
            threadPoolProperties = {
                    @HystrixProperty(name = "coreSize", value = "2"),
                    @HystrixProperty(name = "maxQueueSize", value = "10")
            },
            fallbackMethod = "getUserFallback"
    )
    public UserMapper getUserByUserName(String userName) {
        ResponseEntity<UserMapper> restExchange = restTemplate.exchange("http://user-service/user/{userName}",
                HttpMethod.GET, null, UserMapper.class, userName);
        return restExchange.getBody();
    }

    private UserMapper getUserFallback(String userName) {
        return new UserMapper(0L,"no_user","not_existed_user");
    }
}
